import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

    def test1(self):
        self.browser.get('http://likeshop:8080/admin/account/login.html')
        sleep(1)
        self.browser.find_element(By.NAME, 'account').send_keys('admin')
        self.browser.find_element(By.NAME, 'password').send_keys('123456')
        self.browser.find_element(By.ID, 'login').click()
        sleep(2)
        # 点击商品按钮
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        # 定位iframe子框架
        iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
        # 定位商品名称搜索框
        self.browser.find_element(By.ID, 'keyword').send_keys('文具')
        # 定位查询按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/div[3]/button[1]').click()
        sleep(2)
        # 商品列表
        shop_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        is_not_bug = True
        if len(shop_list) == 0:
            print('未查询到商品')
        else:
            for shop in shop_list:
                # 商品名称
                shop_name = shop.find_element(By.XPATH, './td[2]/div').text
                if '文具' not in shop_name:
                    print('有BUG')
                    is_not_bug = False
                    break
        if is_not_bug:
            print('没有BUG')

        # 退出子框架
        self.browser.switch_to.default_content()
        sleep(2)
        # 定位用户信息
        df = self.browser.find_element(By.XPATH, '//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/a/cite')
        # 鼠标悬停
        ActionChains(self.browser).move_to_element(df).perform()
        sleep(2)
        # 点击退出
        self.browser.find_element(By.XPATH, '//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
        sleep(2)
                            
                    
   
if __name__ == '__main__':
    test = LikeShopTest()
    test.test1()
